package com.frxuexi.park2212.patrol

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.SearchView.OnQueryTextListener
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.lib_base.entity.PointBeanItem
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivityPointBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/PointActivity")
@AndroidEntryPoint
class PointActivity : BaseActivity<ActivityPointBinding,PatrolViewModel>() {
    val pointAdapter by lazy { PointAdapter() }
    var type="保洁巡检"
    val list= mutableListOf<PointBeanItem>()
    override fun initData() {
        vm.getPoints.observe(this){
            pointAdapter.data.clear()
            list.clear()
            list.addAll(it)
            pointAdapter.addData(list.filter { it.patrolType==type })
            pointAdapter.notifyDataSetChanged()
        }

    }

    override fun initView() {

        binding.rvPatrol.apply {
            adapter=pointAdapter
            layoutManager=LinearLayoutManager(this@PointActivity)
        }

        pointAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/PointDetailActivity")
                .withString("pid",pointAdapter.data.get(position).id.toString()).navigation()
        }

        val arr = arrayOf("保洁巡检", "日常巡检", "安防巡检","家居巡检")
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, arr)
        binding.patrolStatus.adapter=adapter
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.patrolStatus.onItemSelectedListener=object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                type=arr[position]
                pointAdapter.data.clear()
                pointAdapter.data.addAll(list.filter { it.patrolType==type })
                pointAdapter.notifyDataSetChanged()
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

        }

        binding.patrolSv.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
//                vm.getPointsByType(query!!,type)
                vm.getPointsByType("D座C栋",type)
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {

                return true
            }

        })
    }


    override fun onResume() {
        super.onResume()
        vm.getPoints()
    }
}